home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Science / RLaB / rlib / eval.r < prev    next >
Text File  |  1994-04-25  |  2KB  |  56 lines

  1. //-------------------------------------------------------------------//
  2.  
  3. //  Syntax:    eval ( S )
  4.  
  5. //  Description:
  6.  
  7. //  The eval function evaluates the expression contained in the string
  8. //  argument S. Eval does not return the value of expression. Instead,
  9. //  the expression, or statement is evaluated in a global context. Any
  10. //  assignments, or variable modifications will effect the global
  11. //  symbol table only.
  12.  
  13. //  Eval, since it evaluates S on the fly, does not have any knowledge
  14. //  of function arguments, or local variables. Thus if you use eval
  15. //  within another function, you must be careful not to reference
  16. //  function arguments or local variables (however, you may use them
  17. //  to construct eval's argument). An example of this type of misuse
  18. //  is presented below.
  19.  
  20. //  > x = function ( s, a )
  21. //    {
  22. //      eval (s);
  23. //    }
  24. //  >
  25. //  > x ("2*a", 3)
  26. //  ./rlab: a, UNDEFINED
  27. //  near line 2, file: /usr/local/lib/rlab/rlib/eval.r
  28.  
  29. //  The above example would work if the undefined variable `a' were
  30. //  not referenced. For example we could do:
  31.  
  32. //  > x ("y = sqrt (3)", NOOP);
  33. //   y =
  34. //       1.73
  35.  
  36. //  Usually, most things that you would need an eval function for (in
  37. //  another language) can be done with strings, of lists in RLaB.
  38. //-------------------------------------------------------------------//
  39.  
  40. eval = function ( S )
  41. {
  42.   local (tmpf, tmps);
  43.  
  44.   if (!exist (S)) { return 0; }
  45.   if (class (S) != "string")
  46.   {
  47.     error ("eval: requires string argument");
  48.   }
  49.  
  50.   tmpf = tmp_file ();
  51.   fprintf (tmpf, "%s\n", S[1]);
  52.   close (tmpf);
  53.   load (tmpf);
  54.   system ("rm -f " + tmpf);
  55. };
  56.